package ink.ykb.actDemo.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import ink.ykb.actDemo.controller.vo.UserTask;
import ink.ykb.actDemo.entity.Leave;
import ink.ykb.actDemo.service.LeaveService;
import ink.ykb.actDemo.util.CommonResult;

@RequestMapping("/user")
@Controller
public class UserController {

	@Autowired
    private LeaveService leaveService;

	/**
	 * 部署流程
	 */
    @RequestMapping("/deploy")
    @ResponseBody
    public CommonResult deploy() {
    	leaveService.deploy();
    	return CommonResult.resultSuccess("流程部署成功");
    }
	
	/**
	 * 保存请假信息
	 * @param leave
	 * @return
	 */
    @RequestMapping("/save")
    @ResponseBody
    public CommonResult save(Leave leave) {
    	leaveService.save(leave);
    	return CommonResult.resultSuccess("请假信息保存成功");
    }
    @RequestMapping("/leaveList")
    @ResponseBody
    public CommonResult getLeaveList() {
    	List<Leave> leaveList = leaveService.getLeaveList();
    	return CommonResult.resultSuccess(leaveList, leaveList.size());
    }
  
    /**
     * 查看流程图
     * @param leaveId
     */
    @RequestMapping(value = "/showImg")  
    public void showImg(HttpServletResponse response,String leaveId){  
        leaveService.showImg(response, leaveId);
    }
    
    /**
     * 获取任务列表
     * @param leaveId
     * @return
     */
    @RequestMapping(value = "/task/list")
    @ResponseBody
    public CommonResult taskList(Integer leaveId){
        List<UserTask> userTask = leaveService.getUserTask(leaveId);
        return CommonResult.resultSuccess(userTask, userTask.size());
    }
    
    /**
     * 提交请假申请
     * @param leaveId
     * @return
     */
    @RequestMapping(value = "/applyLeave")
    @ResponseBody
    public CommonResult applyLeave(Integer leaveId) {
    	leaveService.applyLeave(leaveId);
    	return CommonResult.resultSuccess("请假申请成功，正在审批中，请等待");
    }
    /**
     * 取消请假申请，需要在被驳回后才能取消申请
     * @param leaveId
     * @return
     */
    @RequestMapping(value = "/cancleLeave")
    @ResponseBody
    public CommonResult cancleLeave(Integer leaveId) {
    	leaveService.cancleLeave(leaveId);
    	return CommonResult.resultSuccess("请假申请取消成功");
    }
    
    /**
     * 审批
     * @param taskId
     * @return
     */
    @RequestMapping(value = "/review")
    @ResponseBody
    public CommonResult review(Integer leaveId,String taskId,Boolean passLeave,String cause) {
    	leaveService.review(leaveId,taskId,passLeave,cause);
    	return CommonResult.resultSuccess("审批成功");
    }
    
    
}
